package com.smooth.act;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;

/**
 * Android实现左右滑动效果
 * 
 * @author yaojuxian
 * 
 */
public class SmoothActivity extends Activity implements OnGestureListener {

	private int SCREEN_WIDTH;

	private ViewFlipper flipper;

	private GestureDetector detector;

	private View startView;

	private View endView;
	
	private Map<String,Long> timeMap;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 获取屏幕的宽度
		SCREEN_WIDTH = getWindowManager().getDefaultDisplay().getWidth();
		
		timeMap = new HashMap<String,Long>();

		// 设置用户手势监听
		detector = new GestureDetector(this);

//		 设置翻页需要的页面,并定义第一页和最后一页
//		 flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper);
//		
//		 startView = addImageView(R.drawable.one);
//		 flipper.addView(startView);
//		 flipper.addView(addImageView(R.drawable.two));
//		 flipper.addView(addImageView(R.drawable.three));
//		 endView = addImageView(R.drawable.four);
//		 flipper.addView(endView);
		//
		//
		// pageView = new PageIndicatorView(this);
		// pageView.setTotalPage(4);
		//
		// // ArrayList<View> list = new ArrayList<View>();
		// // list.add(pageView);
		// //
		// // View view = this.findViewById(R.id.pageView);
		// // view.addTouchables(list);
		// // flipper.addView(pageView);
		//
		// setContentView(pageView);
		//
		// Gallery gallery = (Gallery)findViewById(R.id.gallery);
		// gallery.setOnItemSelectedListener(selectListener);
		// setContentView(gallery);

	}

//	private OnItemSelectedListener selectListener = new OnItemSelectedListener() {
//		@Override
//		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
//				long arg3) {
//			pageView.setCurrentPage(arg2);
//		}
//
//		@Override
//		public void onNothingSelected(AdapterView<?> arg0) {
//			// TODO Auto-generated method stub
//
//		}
//	};

	private View addImageView(int id) {
		ImageView imageView = new ImageView(this);
		imageView.setImageResource(id);
		return imageView;
	}

	/**
	 * 调用OnGestureListener的onTouchEvent,来分析是否有合适的 callback函数来处理用户的手势
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		return detector.onTouchEvent(event);
	}

	/**
	 * 用户按下触摸屏,快速移动后松开,由一个MotionEvent ACTION_DOWN事件,
	 * 多个ACTION_MOVE事件,一个ACTION_UP事件触发
	 */
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {

		if (e1.getX() - e2.getX() > 120) {
			return moveRight();
		} else if (e1.getX() - e2.getX() < -120) {
			return moveLeft();
		}
		return false;
	}

	/**
	 * 用户轻触触摸屏,有一个MotionEvent ACTION_DOWN事件触发
	 */
	@Override
	public boolean onDown(MotionEvent e) {

		return false;
	}

	/**
	 * 用户轻触屏幕,尚未松开或拖动,由一个MotionEvent ACTION_DOWN事件触发
	 */
	@Override
	public void onShowPress(MotionEvent e) {
		// Toast.makeText(this, "你还没松开屏幕哦.",
		// Toast.LENGTH_SHORT).show();
	}

	/**
	 * 用户(轻触触摸屏后)松开,由一个MotionEvent ACTION_UP事件触发
	 */
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// Toast.makeText(this, "你松开了屏幕.",
		// Toast.LENGTH_SHORT).show();
		if (e.getX() < SCREEN_WIDTH / 2) {
			// 用户点击左半边屏幕
			return moveLeft();
		} else if (e.getX() > SCREEN_WIDTH / 2) {
			// 用户点击右半边屏幕
			return moveRight();
		}
		return false;
	}

	/**
	 * 用户按下触摸屏,并拖动,由一个MotionEvent ACTION_DOWN事件, 多个ACTION_MOVE事件触发
	 */
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	/**
	 * 用户长按触摸屏,由多个MotionEvent ACTION_DOWN事件触发
	 */
	@Override
	public void onLongPress(MotionEvent e) {

	}

	/**
	 * 向右移
	 * 
	 * @return
	 */
	private boolean moveRight() {

		flipper.setInAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_left_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_left_out));

		if (flipper.getCurrentView().equals(endView)) {
			Long time = timeMap.get("");
			Toast.makeText(this, getString(R.string.is_last),
					Toast.LENGTH_SHORT).show();
		} else {
			flipper.showNext();
		}
		return true;
	}

	/**
	 * 向左移
	 * 
	 * @return
	 */
	private boolean moveLeft() {

		flipper.setInAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_right_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
				R.anim.push_right_out));
		if (flipper.getCurrentView().equals(startView)) {
			Toast.makeText(this, getString(R.string.is_first),
					Toast.LENGTH_SHORT).show();
		} else {
			flipper.showPrevious();
		}
		return true;
	}
}